package com.tchy.cloud.controller;

import com.tchy.cloud.entity.AOfficialWhEntity;
import com.tchy.cloud.entity.AStudioEntity;
import jodd.bean.BeanUtil;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.DataGridReturn;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSBaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 工作室
 */
@Controller
@RequestMapping("/officialController")
public class AOfficialController {
    private static final Logger logger = Logger.getLogger(AOfficialWhEntity.class.getName());

    @Autowired
    private CommonService commonService;

    /**
     * 工作室审核列表展示页面
     *
     * @return
     */
    @RequestMapping(params = "review_index")
    public ModelAndView indexView() {
        return new ModelAndView("clound/official/review_index");
    }


    /**
     * //     * 文章列表展示数据
     * //     *
     * //     * @param studioEntity
     * //     * @param request
     * //     * @param response
     * //     * @param dataGrid
     * //
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(AOfficialWhEntity officialWhEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(AOfficialWhEntity.class, dataGrid);
        cq.addOrder("articleId", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, officialWhEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        DataGrid dg = cq.getDataGrid();
        getDataGrid(dg);
        TagUtil.datagrid(response, dataGrid);
    }

    private DataGrid getDataGrid(DataGrid dg) {
        String sql = "select t2.title from a_official_columns t1\n" +
                "INNER JOIN a_columns t2 on t1.columns_id = t2.columns_id\n" +
                "where t1.article_id = ?";
        List list = dg.getResults();
        for (int i = 0; i < list.size(); i++) {
            AOfficialWhEntity official = (AOfficialWhEntity) list.get(i);
            Long articleId = official.getArticleId();
            List<Map<String, Object>> alist = commonService.findForJdbc(sql, new Object[]{articleId});
            String text = "";
            for (int j = 0; j < alist.size(); j++) {
                text += alist.get(j).get("title");
                if (j != alist.size() - 1)
                    text += ",";
            }
            official.setTitleText(text);
        }
        return dg;
    }

    /**
     * 文章审核页面跳转
     *
     * @param officialWhEntity
     * @param request
     * @return
     */
    @RequestMapping(params = "review")
    public ModelAndView review(AOfficialWhEntity officialWhEntity, HttpServletRequest request) {
        String articleId = request.getParameter("id");
        if (null != articleId) {
            officialWhEntity = commonService.getEntity(AOfficialWhEntity.class, Long.valueOf(articleId));
        }
        request.setAttribute("officialWhEntity", officialWhEntity);
        return new ModelAndView("clound/official/review");
    }

    /**
     * 文章审核确认
     *
     * @param officialWhEntity
     * @param req
     * @return
     */
    @RequestMapping(params = "reviewSubmit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson reviewSubmit(AOfficialWhEntity officialWhEntity, HttpServletRequest req) {
        AjaxJson j = new AjaxJson();
        if (officialWhEntity.getArticleId() != null) {
            try {
                AOfficialWhEntity entity = commonService.getEntity(AOfficialWhEntity.class, officialWhEntity.getArticleId());
                MyBeanUtils.copyBeanNotNull2Bean(officialWhEntity, entity);
                entity.setPublishTime(new java.util.Date());
                entity.setPublisher(ResourceUtil.getSessionUserName());

                commonService.saveOrUpdate(entity);
                j.setMsg("操作成功");
            } catch (Exception e) {
                logger.error(e);
                j.setMsg("操作失败");
            }
        } else {
            j.setMsg("工作室不存在。");
        }
        return j;

    }


    @RequestMapping(params = "articleInfo")
    public ModelAndView articleInfo(AOfficialWhEntity officialWhEntity, HttpServletRequest request) {
        String articleId = request.getParameter("id");
        if (null != articleId) {
            officialWhEntity = commonService.getEntity(AOfficialWhEntity.class, Long.valueOf(articleId));
            String sql = "select group_concat(t2.title separator ',') as title from a_official_columns t1\n" +
                    "INNER JOIN a_columns t2 on t1.columns_id = t2.columns_id\n" +
                    "where t1.article_id = ?";
            Map<String, Object> map = commonService.findOneForJdbc(sql, new Object[]{officialWhEntity.getArticleId()});
            String text = "";
            if (!map.isEmpty())
                text += String.valueOf(map.get("title"));
            officialWhEntity.setTitleText(text);
        }
        request.setAttribute("officialWhEntity", officialWhEntity);
        return new ModelAndView("clound/official/articleInfo");
    }
//
//
//
//    /**
//     * 工作室管理列表展示数据
//     *
//     * @param studioEntity
//     * @param request
//     * @param response
//     * @param dataGrid
//     */
//    @RequestMapping(params = "datagrid2")
//    public void datagrid2(AStudioEntity studioEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
//        CriteriaQuery cq = new CriteriaQuery(AStudioEntity.class, dataGrid);
//        String createName = request.getParameter("consultantEntity.aCustomerEntity.realname");
//        if (StringUtil.isNotEmpty(createName)) {
//            cq.createAlias("consultantEntity.aCustomerEntity", "as_customerEntity");
//            cq.like("as_customerEntity.realname", '%' + createName + '%');
//            studioEntity.getConsultantEntity().getaCustomerEntity().setRealname(null);
//        }
//        cq.notEq("isReview", 11);
//
//        cq.addOrder("studioId", SortDirection.desc);
//        cq.add();
//        // 查询条件组装
//        HqlGenerateUtil.installHql(cq, studioEntity, request.getParameterMap());
//        commonService.getDataGridReturn(cq, true);
//        TagUtil.datagrid(response, dataGrid);
//    }
//
//    /**
//     * 工作室审核页面
//     *
//     * @param studioEntity
//     * @param req
//     * @return
//     */
//    @RequestMapping(params = "review")
//    public ModelAndView review(AStudioEntity studioEntity, HttpServletRequest req) {
//        String studioId = req.getParameter("id");
//        if (null != studioId) {
//            studioEntity = commonService.getEntity(AStudioEntity.class, Long.valueOf(studioId));
//            String sql = "";
//        }
//        req.setAttribute("studioEntity", studioEntity);
//        if (req.getParameter("type").equals("0"))
//            return new ModelAndView("clound/studio/reviewView");
//        else
//            return new ModelAndView("clound/studio/reviewShowView");
//    }
//
//    /**
//     * 工作室审核功能
//     *
//     * @param studioEntity
//     * @return
//     */
//    @RequestMapping(params = "reviewSubmit", method = RequestMethod.POST)
//    @ResponseBody
//    public AjaxJson reviewSubmit(AStudioEntity studioEntity) {
//        AjaxJson j = new AjaxJson();
//        try {
//            if (studioEntity.getStudioId() != null) {//修改
//                AStudioEntity entity = commonService.get(AStudioEntity.class, studioEntity.getStudioId());
//                MyBeanUtils.copyBeanNotNull2Bean(studioEntity, entity);
//                commonService.saveOrUpdate(entity);
//            } else {//保存
//                commonService.save(studioEntity);
//            }
//            j.setMsg("操作成功");
//        } catch (Exception e) {
//            logger.error(e);
//            j.setMsg("操作失败");
//        }
//        return j;
//    }
//
//
//    /**
//     * 排序保存
//     *
//     * @param studioEntity
//     * @return
//     */
//    @RequestMapping(params = "sortSubmit", method = RequestMethod.POST)
//    @ResponseBody
//    public AjaxJson sortSubmit(AStudioEntity studioEntity, HttpServletRequest req) {
//        AjaxJson j = new AjaxJson();
//        if (studioEntity.getStudioId() != null) {
//            try {
//                AStudioEntity entity = commonService.getEntity(AStudioEntity.class, studioEntity.getStudioId());
//                MyBeanUtils.copyBeanNotNull2Bean(studioEntity, entity);
//                commonService.saveOrUpdate(entity);
//                j.setMsg("操作成功");
//            } catch (Exception e) {
//                logger.error(e);
//                j.setMsg("操作失败");
//            }
//        }else{
//            j.setMsg("工作室不存在。");
//        }
//        return j;
//
//    }
//
//    /**
//     * 工作室解禁和封禁
//     *
//     * @param studioEntity
//     * @return
//     */
//    @RequestMapping(params = "forbidden", method = RequestMethod.POST)
//    @ResponseBody
//    public AjaxJson forbidden(AStudioEntity studioEntity, HttpServletRequest req) {
//        AjaxJson j = new AjaxJson();
//        String studioId = req.getParameter("id");
//        try {
//            if (null != studioId) {
//                studioEntity = commonService.getEntity(AStudioEntity.class, Long.valueOf(studioId));
//                if (studioEntity.getIsAuth() == 0)
//                    studioEntity.setIsAuth(1);
//                else
//                    studioEntity.setIsAuth(0);
//            }
//            commonService.saveOrUpdate(studioEntity);
//            j.setMsg("操作成功");
//        } catch (Exception e) {
//            logger.error(e);
//            j.setMsg("操作失败");
//        }
//        return j;
//
//    }
//
//
////
////    /**
////     * 新增或编辑页面跳转
////     *
////     * @param aBankEntity
////     * @param req
////     * @return
////     */
////    @RequestMapping(params = "addorupdateView")
////    public ModelAndView addorupdateView(ABankEntity aBankEntity, HttpServletRequest req) {
////        if (aBankEntity.getId() != null) {
////            aBankEntity = commonService.getEntity(ABankEntity.class, aBankEntity.getId());
////        }
////        req.setAttribute("bank", aBankEntity);
////        return new ModelAndView("clound/bank/addorupdateView");
////    }
////
////
////    /**
////     * 新增及修改的数据处理
////     * @param aBankEntity
////     * @return
////     */
////    @RequestMapping(params = "saveorupdate", method = RequestMethod.POST)
////    @ResponseBody
////    public AjaxJson saveorupdate(ABankEntity aBankEntity) {
////        AjaxJson j = new AjaxJson();
////        try {
////            if (aBankEntity.getId() != null) {//修改
////                ABankEntity entity = commonService.get(ABankEntity.class, aBankEntity.getId());
////                MyBeanUtils.copyBeanNotNull2Bean(aBankEntity, entity);
////                commonService.saveOrUpdate(entity);
////            } else {//保存
////                commonService.save(aBankEntity);
////            }
////            j.setMsg("操作成功");
////        } catch (Exception e) {
////            logger.error(e);
////            j.setMsg("操作失败");
////        }
////        return j;
////    }

}







